﻿using GrapeCity.ActiveReports.Design;
using GrapeCity.ActiveReports.Document;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GrapeCity.ActiveReports.Samples.CreateReport
{
    public partial class ReportViewer : Form
    {
        public ReportViewer()
        {
            InitializeComponent();
        }

        public Object Report
        { get; set; }

        public DesignerReportType ReportType
        { get; set; }


        private void ReportViewer_Load(object sender, EventArgs e)
        {
            switch (ReportType)
            {
                case DesignerReportType.Page:
                    PageReport report1 = Report as PageReport;
                   
                    viewer1.LoadDocument(report1.Document);
                    break;
                case DesignerReportType.Rdl:
                    PageReport report2 = Report as PageReport;
                    PageDocument reportD = new PageDocument(report2);
                    reportD.LocateDataSource += new LocateDataSourceEventHandler(LoadDataSet);
                    viewer1.LoadDocument(reportD);
                    break;
                    
                case DesignerReportType.Section:
                    SectionReport report3 = Report as SectionReport;
                    viewer1.Document = report3.Document;
                    report3.Run();
                    break;
                default:
                    break;
            }
        }

        private void LoadDataSet(object sender, LocateDataSourceEventArgs args)
        {
           string constr = @"Provider=sqloledb; password=xA123456;data source=10.32.2.28;initial catalog=NWind_CHS;user id=sa;";
           
            //此处修改为AR的测试数据库

           // 创建DataSet
           DataSet myDataSet = new DataSet();
           //string connStr = Properties.Resources.ConnectionString;
           OleDbConnection conn = new OleDbConnection(constr);
           DataTable[] myDataTables = new DataTable[3];
           myDataTables[0] = new DataTable(Constants.SaleTableName);
           myDataTables[1] = new DataTable(Constants.SaleDetailsTableName);
           myDataTables[2] = new DataTable(Constants.CustomerTableName);
           
            //创建DataTable    
           myDataSet.Tables.Add(myDataTables[0]);
           OleDbCommand cmd1 = new OleDbCommand(Constants.cmdText1, conn);
           OleDbDataAdapter oleAdapter1 = new OleDbDataAdapter(cmd1);
           oleAdapter1.Fill(myDataSet.Tables[0]);

           //为Table 添加数据
           myDataSet.Tables.Add(myDataTables[1]);
           OleDbCommand cmd2 = new OleDbCommand(Constants.cmdText2, conn);
           OleDbDataAdapter oleAdapter2 = new OleDbDataAdapter(cmd2);
           oleAdapter2.Fill(myDataSet.Tables[1]);

           //为Table 添加数据
           myDataSet.Tables.Add(myDataTables[2]);
           OleDbCommand cmd3 = new OleDbCommand(Constants.cmdText3, conn);
           OleDbDataAdapter oleAdapter3 = new OleDbDataAdapter(cmd3);
           oleAdapter3.Fill(myDataSet.Tables[2]);


           //创建 “DataRelation Customers_Orders”
           DataRelation Customers_Orders = new DataRelation("C_O",myDataSet.Tables[2].Columns["客户ID"],
               myDataSet.Tables[0].Columns["客户ID"] );
           myDataSet.Relations.Add(Customers_Orders);
           
           //创建 “DataRelation ”
           DataRelation Orders_OrderDetails = new DataRelation("O_OD ", myDataSet.Tables[0].Columns["订单ID"],
                              myDataSet.Tables[1].Columns["订单ID"]);
           myDataSet.Relations.Add(Orders_OrderDetails);
           


          
          
            //返回从表数据
            args.Data = myDataSet.Tables[0];




           

        }
    }
}


